package design.model.chain;

import lombok.Getter;

import java.util.Objects;

/**
 * @author: Eternity.麒麟
 * @description: 抽象处理者
 * @date: 2022/4/10 19:05
 * @version: 1.0
 */
@Getter
public abstract class Handler {

    private Handler handler;

    /**
     * 处理事务
     * @return Result
     */
    public abstract Result execute();

    protected void add(Handler handler) {
        this.handler = handler;
    }

    public static class Builder {
        private Handler head;
        private Handler tail;
        public Builder add(Handler handler) {
            if (Objects.isNull(this.head)) {
                this.head = this.tail = handler;
                return this;
            }
            this.tail.add(handler);
            this.tail = handler;
            return this;
        }

        public Handler build() {
            return this.head;
        }
    }

}
